package com.jalarbee.web.jsf.controller;


import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import com.jalarbee.stock.category.business.Category;
import com.jalarbee.stock.category.service.CategoryService;
import java.util.Set;

public class CategoriesController {

	private CategoryService categoryService;
	private Set<Category> categories;
	
	private void fetchList() {
		categories = categoryService.getAllCategories(null);
	}
	
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public List<SelectItem> getList() {
		if(categories == null) {
			fetchList();
		}
		List<SelectItem> items = new ArrayList<SelectItem>();
		SelectItem item = null;
		for(Category category : categories) {
			item = new SelectItem( category, category.getName());
			items.add(item);
		}
		return items;
	}
}
